Feature: The user can insert and delete nodes

  Background: I have an XML file opened with sample data for all node types
    Given a file named "example.xml" with:
    """
    <?xml version="qwe" encoding="asd"?>
    <root>
      <!--comment-->
      plain text1
      plain text2
      <el1/>
      <elemenwithchildren>
        <child1/>
        <child2/>
      </elemenwithchildren>
    </root>
    """
    When I launch XML Editor interactively
    And I type "1"
    And I type "example.xml"
    And I type "2"

  @insertion
  Scenario: When I try to insert a new node to a comment node I get an error
    When I type "5,8" in succession
    Then the cursor should be `~~> <!--comment-->`
    And the output should contain "! Cannot insert new child node to this type of node!"

  @insertion
  Scenario: When I try to insert a new node to a plain text I get an error
    When I type "5,5,8" in succession
    Then the cursor should be `~~> plain text1`
    And the output should contain "! Cannot insert new child node to this type of node!"

  @insertion
  Scenario: When I try to insert a new node after the rood node get an error
    When I type "2" in succession
    Then the cursor should be `~~> <root>`
    And the output should contain "! Cannot insert another node at the root level!"

  @insertion
  Scenario: When I try to insert a new node to a plain text I get an error
    When I type "1" in succession
    Then the cursor should be `~~> <root>`
    And the output should contain "! Cannot insert another node at the root level!"

  @insertion
  Scenario: When I try to insert a new node before a non-root node I get a type selection menu
    When I type "5,1,1" in succession
    And I close the stdin stream
    And the output should contain:
    """
      (1) Element
      (2) Comment
      (3) Plain text
    ~ Please select the new node type:
    """

  @insertion
  Scenario: When I try to insert a new node after a non-root node I get a type selection menu
    When I type "5,2,1" in succession
    And I close the stdin stream
    And the output should contain:
    """
      (1) Element
      (2) Comment
      (3) Plain text
    ~ Please select the new node type:
    """

  @insertion
  Scenario: When I try to insert a new child node under a parent node I get a type selection menu
    When I type "5,5,5,5,2,1" in succession
    And I close the stdin stream
    And the output should contain:
    """
      (1) Element
      (2) Comment
      (3) Plain text
    ~ Please select the new node type:
    """

  @insertion
  Scenario: When I insert a new child node under a parent node I get a notification about the success
    When I type "5,5,5,5,8,1,a" in succession
    And I close the stdin stream
    And the output should contain:
    """
    ~ Node inserted with default attributes.
    ! Use editing options to change default attributes.
    ~ The cursor is moved to the new node:

    ~~> <element/>
    """
    And the output should contain:
    """
          <el1>
    ~~>     <element/>
          </el1>
    """

  @insertion
  Scenario: When I insert a new child node before a non-root node I get a notification about the success
    When I type "5,1,3,a" in succession
    And I close the stdin stream
    And the output should contain:
    """
    ~ Node inserted before cursor with default attributes.
    ! Use editing options to change default attributes.
    ~ The cursor is moved to the new node:

    ~~> plain text
    """
    And the output should contain:
    """
        <root>
    ~~>   plain text
          <!--comment-->
    """

  @insertion
  Scenario: When I insert a new child node after a non-root node I get a notification about the success
    When I type "5,2,2,a" in succession
    And I close the stdin stream
    And the output should contain:
    """
    ~ Node inserted after cursor with default attributes.
    ! Use editing options to change default attributes.
    ~ The cursor is moved to the new node:

    ~~> <!--commenttext-->
    """
    And the output should contain:
    """
         <!--comment-->
    ~~>   <!--commenttext-->
          plain text1
    """

  @curr
  @deletion
  Scenario: When I try to delete the root node I get an error
    When I type "9,y" in succession
    And I close the stdin stream
    And the output should contain "! The root node cannot be deleted!"

  @curr
  @deletion
  Scenario: When I delete a parent node all subnodes including the cursor is deleted and I get a notification about the success
    When I type "5,5,5,5,5,9,y,a" in succession
    Then the cursor should be `~~> <root>`
    And the output should contain:
    """
    ~~> <root>
          <!--comment-->
          plain text1
          plain text2
          <el1/>
        </root>
    """

  @curr
  @deletion
  Scenario: When I delete a leaf node I get a notification about the success
    When I type "5,9,y,a" in succession
    Then the cursor should be `~~> <root>`
    And the output should contain:
    """
    ~~> <root>
          plain text1
          plain text2
    """
